package com.phuc.game.andPuzzle.sprite;

import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.ParallelEntityModifier;
import org.anddev.andengine.entity.modifier.PathModifier;
import org.anddev.andengine.entity.modifier.PathModifier.Path;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.util.constants.TimeConstants;

import com.phuc.game.andPuzzle.engine.GamePlay;

public class Puzzle extends AnimatedSprite implements TimeConstants {

	public static final int SIZE_W = 108;
	public static final int SIZE_H = 111;
	public Cell cell;
	int index;
	
	public Puzzle(int tileIndex, int index) {
		super(0, 0, SIZE_W, SIZE_H, GamePlay.textureImage.getFaceTiledTextureRegion());
		this.setCurrentTileIndex(tileIndex);
		this.index = index;
	}
	
	public void moveToBlankCell() {
		final Cell cell = GamePlay.getInstant().getCellBlank();
		if (isNextToCell(cell)) {
			GamePlay.getInstant().setBlankCell(this.cell);
			Path path = new Path(2).to(this.getX(), this.getY()).to(cell.getX(), cell.getY());
			PathModifier pm = new PathModifier(0.1f, path, new PathModifier.IPathModifierListener() {
				@Override
				public void onPathWaypointStarted(PathModifier pPathModifier,
						IEntity pEntity, int pWaypointIndex) { }
				
				@Override
				public void onPathWaypointFinished(PathModifier pPathModifier,
						IEntity pEntity, int pWaypointIndex) { }
				
				@Override
				public void onPathStarted(PathModifier pPathModifier, IEntity pEntity) { }
				
				@Override
				public void onPathFinished(PathModifier pPathModifier, IEntity pEntity) {
					Puzzle.this.cell = cell;
					GamePlay.getInstant().isMoving = false;
				}
			});
			this.registerEntityModifier(new ParallelEntityModifier(pm));
		} else if (isInLineWithCell(cell)) {
			GamePlay.getInstant().setBlankCell(this.cell);
			for (Puzzle puzzle : GamePlay.getInstant().puzzles) {
				// Chay theo duong thang x
				if (this.getX() == cell.getX() && puzzle.getX() == cell.getX()) {
					if (this.getY() <= puzzle.getY() && puzzle.getY() < cell.getY()) {
						puzzle.moveToCell(GamePlay.getInstant().getCell((int)puzzle.getX(), (int)puzzle.getY() + SIZE_H));
					} else if (this.getY() >= puzzle.getY() && puzzle.getY() > cell.getY()) {
						puzzle.moveToCell(GamePlay.getInstant().getCell((int)puzzle.getX(), (int)puzzle.getY() - SIZE_H));
					}
				} else if (this.getY() == cell.getY() && puzzle.getY() == cell.getY()) {
					if (this.getX() <= puzzle.getX() && puzzle.getX() < cell.getX()) {
						puzzle.moveToCell(GamePlay.getInstant().getCell((int)puzzle.getX() + SIZE_W, (int)puzzle.getY()));
					} else if (this.getX() >= puzzle.getX() && puzzle.getX() > cell.getX()) {
						puzzle.moveToCell(GamePlay.getInstant().getCell((int)puzzle.getX() - SIZE_W, (int)puzzle.getY()));
					}
				}
			}
		}
	}
	
	public void moveToCell(final Cell cell) {
		Path path = new Path(2).to(this.getX(), this.getY()).to(cell.getX(), cell.getY());
		PathModifier pm = new PathModifier(0.1f, path, new PathModifier.IPathModifierListener() {
			@Override
			public void onPathWaypointStarted(PathModifier pPathModifier,
					IEntity pEntity, int pWaypointIndex) { }
			
			@Override
			public void onPathWaypointFinished(PathModifier pPathModifier,
					IEntity pEntity, int pWaypointIndex) { }
			
			@Override
			public void onPathStarted(PathModifier pPathModifier, IEntity pEntity) { }
			
			@Override
			public void onPathFinished(PathModifier pPathModifier, IEntity pEntity) {
				Puzzle.this.cell = cell;
				GamePlay.getInstant().isMoving = false;
			}
		});
		this.registerEntityModifier(new ParallelEntityModifier(pm));
	}
	
	public boolean isInLineWithCell(Cell cell) {
		int xPuzzle = (int)this.getX();
		int yPuzzle = (int)this.getY();
		int xCell = cell.getX();
		int yCell = cell.getY();
		if (xPuzzle == xCell || yPuzzle == yCell) {
			return true;
		}
		return false;
	}
	
	public boolean isNextToCell(Cell cell) {
		int xPuzzle = (int)this.getX();
		int yPuzzle = (int)this.getY();
		int xCell = cell.getX();
		int yCell = cell.getY();
		if (xPuzzle == xCell && yPuzzle - yCell == SIZE_H) {
			return true;
		}
		if (xPuzzle == xCell && yPuzzle - yCell == -SIZE_H) {
			return true;
		}
		if (yPuzzle == yCell && xPuzzle - xCell == SIZE_W) {
			return true;
		}
		if (yPuzzle == yCell && xPuzzle - xCell == -SIZE_W) {
			return true;
		}
		return false;
	}
	
	@Override
	public void onManagedUpdate(final float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
	}
	
	public boolean isInCorrectCell() {
		return this.index == cell.getCellIndex();
	}
	
	public void setCell(Cell cell) {
		this.cell = cell;
		this.setPosition(cell.getX(), cell.getY());
	}

	public boolean isInCell(Cell cell) {
		return this.cell == cell;
	}
	
	public int getIndex() {
		return this.index;
	}
	
}
